<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>zabbix监控mysql数据库状态 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>zabbix监控mysql数据库状态</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Zabbix自己提供的模板可以监控mysql slow queries,mysqlversion,uptime,alive等。</p>
<ol>
<li><p>Zabbix官方提供的监控mysql的模板Template AppMySQL,可以看到相关的Items和key。</p>
</li>
<li><p>把该模板Template App MySQL Link到相关的主机上面,发现Item的Status是不可用的，因为key的值是通过Mysql用户查看”showglobal status”信息或者用mysqladmin命令查看status或extended-status的信息而取的值。</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show global status; </div><div class="line">mysql&gt; show status;</div></pre></td></tr></table></figure>
<ol>
<li>结合官方提供的key编写Shell脚本，从数据库中取出Items的key的值。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#Create by huxianglin 2015.04.14</span></div><div class="line">MYSQL_SOCK=<span class="string">"/tmp/mysql.sock"</span></div><div class="line">MYSQL_PWD=xxxxxx</div><div class="line"><span class="built_in">source</span> /etc/profile.d/mysqld.sh</div><div class="line">ARGS=1</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -ne <span class="string">"<span class="variable">$ARGS</span>"</span> ];<span class="keyword">then</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Please input onearguement:"</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">Uptime)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> status|cut -f2 -d<span class="string">":"</span>|cut -f1 -d<span class="string">"T"</span>`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_update)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_update"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Slow_queries)</div><div class="line">result=`mysqladmin -uroot -p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> status |cut -f5 -d<span class="string">":"</span>|cut -f1 -d<span class="string">"O"</span>`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_select)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_select"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_rollback)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_rollback"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Questions)</div><div class="line">result=`mysqladmin -uroot -p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> status|cut -f4 -d<span class="string">":"</span>|cut -f1 -d<span class="string">"S"</span>`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_insert)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_insert"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_delete)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_delete"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_commit)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_commit"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Bytes_sent)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Bytes_sent"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Bytes_received)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Bytes_received"</span> |cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line">Com_begin)</div><div class="line">result=`mysqladmin -uroot-p<span class="variable">$&#123;MYSQL_PWD&#125;</span> -S <span class="variable">$MYSQL_SOCK</span> extended-status |grep -w <span class="string">"Com_begin"</span>|cut -d<span class="string">"|"</span> -f3`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$result</span></div><div class="line">;;</div><div class="line"></div><div class="line">*)</div><div class="line"><span class="built_in">echo</span><span class="string">"Usage:<span class="variable">$0</span>(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"</span></div><div class="line">;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>修改权限chmod 755 /etc/zabbix-2.4.4/scripts/checkmysqlperformance.sh</strong></p>
<ol>
<li>在Zabbix_agentd.conf里面添加UserParameter，格式如下，对于Zabbix来说，脚本其实就是一个插件。</li>
</ol>
<ul>
<li>UserParameter=mysql.version,mysql -V </li>
<li>UserParameter=mysql.ping,mysqladmin -uroot -pxxxxxx -S /tmp/mysql.sock ping | grep -c alive </li>
<li>UserParameter=mysql.status[*],/etc/zabbix-2.4.4/scripts/checkmysqlperformance.sh $1 $2 </li>
<li>重启agentd服务器，然后在zabbix server中添加模板Template AppMySQL。 </li>
<li>在zabbix前端可以实时查看SQL语句每秒钟的操作次数。</li>
</ul>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E7%9B%91%E6%8E%A7mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%8A%B6%E6%80%81/01.jpeg?raw=true" alt=""></figure></p>
<ul>
<li>在zabbix前端可以实时查看mysql发送接收的字节数。其中bytes received表示从所有客户端接收到的字节数，bytes sent表示发送给所有客户端的字节数。</li>
</ul>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E7%9B%91%E6%8E%A7mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%8A%B6%E6%80%81/02.jpeg?raw=true" alt=""></figure></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把该脚本放到要监控的服务器上面（Modifymysql user and password），修改UserParameter的参数并重启agentd,Link官方提供的Template App MySQL模板即可。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里是测试环境用root账号，线上服务器安全期间可以给mysql用户授权readonly权限。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;根据实际的需求，除了监控上述监控项之外，还可以监控mysqlprocesslist,Innodb等。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/14. zabbix监控mysql数据库状态/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
